package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.HouseCommunityTMapper;
import com.ruoyi.community.domain.HouseCommunityT;
import com.ruoyi.community.service.IHouseCommunityTService;

/**
 * 社区Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-20
 */
@Service
public class HouseCommunityTServiceImpl implements IHouseCommunityTService 
{
    @Autowired
    private HouseCommunityTMapper houseCommunityTMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询社区
     * 
     * @param communityId 社区主键
     * @return 社区
     */
    @Override
    public HouseCommunityT selectHouseCommunityTByCommunityId(Long communityId)
    {
        return houseCommunityTMapper.selectHouseCommunityTByCommunityId(communityId);
    }

    /**
     * 查询社区列表
     * 
     * @param houseCommunityT 社区
     * @return 社区
     */
    @Override
    public List<HouseCommunityT> selectHouseCommunityTList(HouseCommunityT houseCommunityT)
    {
        return houseCommunityTMapper.selectHouseCommunityTList(houseCommunityT);
    }

    /**
     * 新增社区
     * 
     * @param houseCommunityT 社区
     * @return 结果
     */
    @Override
    public int insertHouseCommunityT(HouseCommunityT houseCommunityT)
    {
        SysUser user = new SysUser();
        user.setUserName(houseCommunityT.getCommunityName());
        user.setCreateBy(houseCommunityT.getCreater());
        user.setPassword("123456");
        user.setNickName(houseCommunityT.getCommunityName());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        userMapper.insertUser(user);
        List<SysUserRole> list = new ArrayList<SysUserRole>();
        SysUserRole ur = new SysUserRole();
        ur.setUserId(user.getUserId());
        ur.setRoleId(103L);
        list.add(ur);
        userRoleMapper.batchUserRole(list);
        return houseCommunityTMapper.insertHouseCommunityT(houseCommunityT);
    }

    /**
     * 修改社区
     * 
     * @param houseCommunityT 社区
     * @return 结果
     */
    @Override
    public int updateHouseCommunityT(HouseCommunityT houseCommunityT)
    {
        return houseCommunityTMapper.updateHouseCommunityT(houseCommunityT);
    }

    /**
     * 批量删除社区
     * 
     * @param communityIds 需要删除的社区主键
     * @return 结果
     */
    @Override
    public int deleteHouseCommunityTByCommunityIds(Long[] communityIds)
    {
        return houseCommunityTMapper.deleteHouseCommunityTByCommunityIds(communityIds);
    }

    /**
     * 删除社区信息
     * 
     * @param communityId 社区主键
     * @return 结果
     */
    @Override
    public int deleteHouseCommunityTByCommunityId(Long communityId)
    {
        return houseCommunityTMapper.deleteHouseCommunityTByCommunityId(communityId);
    }
}
